package com.huanf.media.api;


import com.huanf.domain.dto.UploadFileParamsDto;
import com.huanf.domain.dto.UploadFileResultDto;
import com.huanf.media.service.MediaFilesService;
import com.huanf.domain.dto.QueryMediaParamsDto;
import com.huanf.domain.entity.MediaFiles;
import com.xuecheng.base.enums.CourseEnum;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author: 35238
 * 功能: 媒资文件管理
 * 时间: 2024-04-15 22:09
 */
@Api(value = "媒资文件管理接口",tags = "媒资文件管理接口")
@RestController
public class MediaFilesController {


    @Autowired
    MediaFilesService mediaFileService;


    @ApiOperation("媒资列表查询接口")
    @PostMapping("/files")
    public PageResult<MediaFiles> list(PageParams pageParams, @RequestBody QueryMediaParamsDto queryMediaParamsDto){
        Long companyId = 1232141425l;
        return mediaFileService.queryMediaFiels(companyId,pageParams,queryMediaParamsDto);
    }

    @ApiOperation("上传图片")
    @RequestMapping(value = "/upload/coursefile",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public UploadFileResultDto upload(@RequestPart("filedata")MultipartFile filedata,
                                      @RequestParam(value= "objectName", required = false) String objectName
    ) throws IOException {
        //机构端还没写，这个机构id先写死
        Long companyId = 1232141425L;
        //拿到了filedata这个文件目前存在服务器内存中
        File tempFile = File.createTempFile("minio",".temp");
        filedata.transferTo(tempFile);
        //取得绝对路径
        String absolutePath = tempFile.getAbsolutePath();
        UploadFileParamsDto uploadFileParamsDto = new UploadFileParamsDto();
        //准备上传文件的一些信息
        uploadFileParamsDto.setFilename(filedata.getOriginalFilename());
        uploadFileParamsDto.setFileSize(filedata.getSize());
        //文件类型
        uploadFileParamsDto.setTags(CourseEnum.MINIO_FILE_NO.getValue());

        return mediaFileService.uploadFile(companyId,uploadFileParamsDto,absolutePath,objectName);
    }


}
